#ifndef SERVICEPROVIDER_H
#define SERVICEPROVIDER_H

#include <QObject>
#include <QNetworkReply>
#include <QStringList>
#include <QtNetwork/QAbstractSocket>
#include <QtNetwork/QSslSocket>
#include "../model_view/credit_card/creditcardprofile.h"


QT_BEGIN_NAMESPACE
class QSslSocket;
QT_END_NAMESPACE


class ServiceProvider : public QObject
{
    Q_OBJECT
public:
    explicit ServiceProvider(QObject *parent = 0);
    ServiceProvider(ServiceProvider &other);
    ServiceProvider(const ServiceProvider &other);
    explicit ServiceProvider(QString login, QString password, int parameter=0, QObject *parent = 0);
    ~ServiceProvider();

    enum Operation{
        Balance,
        Payment,
        Auth3DS
    };

    struct PaymentContent{
      CreditCardProfile *card;
      QString           *amount;
    };

    QSsl::SslProtocol       getProtocol();
    Operation               getOperation();
    int                     getUserParameter();
    void                    setProtocol(QSsl::SslProtocol proto);
    void                    setPaymentContent(PaymentContent *content);

    QString&                UserLogin();
    QString&                UserPassword();
    ServiceProvider         & operator =(ServiceProvider &profile);
    enum PaymentResult {
        Success,
        Wait,
        Error
    };
    virtual PaymentResult   StartPayment(CreditCardProfile &card, QString login, QString amount);
static  QString             *findValue(QString &text, const QString key_start, const QString key_end);
static  QString             *findValue(QByteArray &text, const QByteArray key_start, const QByteArray key_end);
protected:
    virtual QByteArray*     PreparePostRequest()=0;
    virtual bool            ParseError(QByteArray &reply)=0;
    virtual void            ParseGetReply(QString &reply)=0;
    virtual void            ParsePostReply(QByteArray &reply)=0;


    QSslSocket              *socket;
    QNetworkAccessManager   *manager;
    QString                 *GetUrlStr;
    QString                 *PostUrl;
    QString                 *ServerUrlStr;
    QSsl::SslProtocol       protocol;

    QString*                userLogin;
    QString*                userPassword;

    int                     userParameter;
    Operation               m_operation;
    PaymentContent          m_paymentContent;

public slots:
    void                    SecureConnect();

private slots:

    void                    SslSocketError(QList<QSslError> err_list);
    void                    socketError(QAbstractSocket::SocketError);
    void                    socketReadyRead();
    void                    Encrypted();
    void                    finishedGetRequestSlot(QNetworkReply* reply);
    void                    finishedPostRequestSlot(QNetworkReply* reply);

signals:
    void                    StageStarted(int row);
    //void                    StageFinished(int row, int percent);
    void                    StageFinished(int percent);
    void                    JobFinished(int row);
    void                    balanaceObtained(QString );
    void                    paymentFinished(QString );

    void                    paymentError(QString );
    void                    warning(QString );
    void                    critical(QString );
public slots:
};

#endif // SERVICEPROVIDER_H
